package com.shengyijie.adapter.asyncimageadapter;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.URL;
import java.util.HashMap;


import com.shengyijie.activity.MainActivity;
import com.shengyijie.activity.R;   
import com.shengyijie.util.Utility; 
 

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;

public class AsyncImageLoader {

	 private HashMap<String, SoftReference<Drawable>> imageCache;
	     public AsyncImageLoader() {
	    	 imageCache = new HashMap<String, SoftReference<Drawable>>();
	     }
	  
	     public Drawable loadDrawable(final String imageUrl, final ImageCallback imageCallback) {
	    	     try{
		            if (imageCache.containsKey(imageUrl)) {
		               SoftReference<Drawable> softReference = imageCache.get(imageUrl);
		               Drawable drawable = softReference.get();
		               if (drawable != null) {
		                 return drawable;
		               }
		            }
	    	     }catch(Exception e){}
		         final Handler handler = new Handler() {
		             public void handleMessage(Message message) {
		                 imageCallback.imageLoaded((Drawable) message.obj, imageUrl);
		             }
		         };
		         new Thread() {
		             @Override
		             public void run() {
		            	 try{
		                    Drawable drawable = loadImageFromUrl(imageUrl);
		                    imageCache.put(imageUrl, new SoftReference<Drawable>(drawable));
		                    Message message = handler.obtainMessage(0, drawable);
		                    handler.sendMessage(message);
		                    drawable=null;
		            	 }catch(Exception e){}
		             }
		         }.start();
	    	 
	         return null;
	     }
	  
		public static Drawable loadImageFromUrl(String url) {
			Drawable d =null;
			try{
				String fileName=Utility.getPictureFileName(url); 
				
				URL m;
				InputStream i = null; 
				try {
					if(!fileName.equals("")){
					   File f = new File(Environment.getExternalStorageDirectory(),"shengyijie/"+fileName);
					   FileInputStream is = null;
					   is = new FileInputStream(f);
					   Bitmap bm;
					   bm = BitmapFactory.decodeStream(is, null, null);
					   d = new BitmapDrawable(bm);
					}
				}catch (Exception e) {}
				
				if(d == null){
					try {
						m = new URL(url);
						i = (InputStream) m.getContent();
						d = Drawable.createFromStream(i, "src");
						i.close();
						m=null;
						i=null;
						if(!fileName.equals("")){
						   Utility.saveImage(fileName,d);
						}
					}catch (Exception e) {}
				}
				if (d == null) { 
						Resources res = MainActivity.group.getResources();
					    d = res.getDrawable(R.drawable.load); 
				}
			}catch(Exception e){}
 
			return d;
		}
	     public interface ImageCallback {
	         public void imageLoaded(Drawable imageDrawable, String imageUrl);
	     }
}
